#include"groupmodel.hpp"
#include"ConnectionPool.h"

// 创建群组
bool GroupModel::createGroup(Group &group)
{
    // 组装sql语句
    char sql[1024] = {0};
    sprintf(sql,"insert into AllGroup(groupname,groupdesc) values('%s','%s')",
         group.getName().c_str(),group.getDesc().c_str());

    ConnectionPool *cp = ConnectionPool::getConnectionPool();
    shared_ptr<Connection> sp = cp->getConnection();

    if(sp->update(sql))
    {
        // 获取插入成功的用户数据生成的主键
        group.setId(mysql_insert_id(sp->getConnection()));
        return true;
    }

    return false;
}

// 将该用户加入特定群组
void GroupModel::addGroup(int userid, int groupid, string role)
{
    // 组装sql语句
    char sql[1024] = {0};
    sprintf(sql,"insert into GroupUser(userid,groupid,grouprole) values(%d,%d,'%s')",
         userid,groupid,role.c_str());

    ConnectionPool *cp = ConnectionPool::getConnectionPool();
    shared_ptr<Connection> sp = cp->getConnection();

    sp->update(sql);

}

// 获取该用户所在 所有群组信息 并将组的组员信息一并存入
vector<Group> GroupModel::queryGroups(int userid)
{
    // 组装sql语句
    char sql[1024] = {0};
    sprintf(sql,"select b.id,b.groupname,b.groupdesc from GroupUser a inner join \
    AllGroup b on b.id = a.groupid where a.userid=%d",userid);

    vector<Group> vec;

    ConnectionPool *cp = ConnectionPool::getConnectionPool();
    shared_ptr<Connection> sp = cp->getConnection();

    MYSQL_RES *res = sp->query(sql);
    if(res != nullptr)
    {
        // 获取userid的所有消息
        MYSQL_ROW row;
        while((row=mysql_fetch_row(res)) != nullptr)
        {
            Group gp;
            gp.setId(atoi(row[0]));
            gp.setName(row[1]);
            gp.setDesc(row[2]);
            vec.push_back(gp);
        }
        mysql_free_result(res);
    }

     

    for(auto& group:vec)
    {
        // groupid -> userid -> userdate
        sprintf(sql,"select a.id,a.name,a.state,b.grouprole from User a inner join \
        GroupUser b on a.id = b.userid where b.groupid=%d",group.getId());

        
        MYSQL_RES *res = sp->query(sql);
        if(res != nullptr)
        {
            // 获取userid的所有消息
            MYSQL_ROW row;
            while((row=mysql_fetch_row(res)) != nullptr)
            {
                GroupUser user;
                user.setId(atoi(row[0]));
                user.setName(row[1]);
                user.setState(row[2]);
                user.setRole(row[3]);
                group.getUsers().push_back(user);
            }
            mysql_free_result(res);
        }

    }

    return vec;
}

// 获取该用户 该群组的 除了该用户以外的 群成员id
vector<int> GroupModel::queryGroupUsers(int userid, int groupid)
{
    char sql[1024] = {0};
    sprintf(sql,"select userid from GroupUser where groupid=%d and userid!=%d",groupid,userid);

    vector<int> vec;

    ConnectionPool *cp = ConnectionPool::getConnectionPool();
    shared_ptr<Connection> sp = cp->getConnection();

    MYSQL_RES *res = sp->query(sql);
    if(res != nullptr)
    {
        // 获取userid的所有消息
        MYSQL_ROW row;
        while((row=mysql_fetch_row(res)) != nullptr)
        {
            vec.push_back(atoi(row[0]));
        }
        mysql_free_result(res);
        return vec;
    }

    

    return vec;
}
